package com.solcoo.merchant.ui.reg;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.common.util.DialogUtil;
import com.android.common.util.StringUtil;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseActivity;
import com.solcoo.merchant.ui.reg.CheckCodeClickListener.CheckCodeCallBack;
import com.solcoo.merchant.ui.reg.CheckCodeClickListener.PhoneNoLoader;

public class RegActivity extends BaseActivity {

	private String phoneNo;

	private String checkcode;

	private EditText codeEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reg);
		phoneNo = getIntent().getStringExtra("phoneNo");
		checkcode = getIntent().getStringExtra("checkcode");
		Log.v(TAG, "验证码：" + checkcode + "手机号：" + phoneNo);
		Button send = (Button) findViewById(R.id.send);
		send.setOnClickListener(sendOnClickListener);
		Button getCheckCode = (Button) findViewById(R.id.get_check_code_btn);
		CheckCodeClickListener mCheckCodeClickListener = new CheckCodeClickListener(
				mPhoneNoLoader, this, callback, getCheckCode);
		mCheckCodeClickListener.startDisabled();
		getCheckCode.setOnClickListener(mCheckCodeClickListener);
		TextView txPhoneNo = (TextView) findViewById(R.id.phone_no);
		txPhoneNo.setText(phoneNo);

		codeEditText = (EditText) findViewById(R.id.check_code_input);
	}

	private OnClickListener sendOnClickListener = new OnClickListener() {

		private Dialog mDialog;

		@Override
		public void onClick(View v) {
			String inputCode = codeEditText.getText().toString();
			if (StringUtil.isBlank(inputCode)) {
				showDialog(null, "请输入验证码");
				return;
			}
			if (checkcode.equalsIgnoreCase(inputCode)) {
				Intent it = new Intent(v.getContext(), RegInfoActivity.class);
				it.putExtra("phoneNo", phoneNo);
				startActivity(it);
			} else {
				showDialog("验证码输入有误", null);
			}

		}

		private void showDialog(String msg, String title) {
			if (mDialog == null) {
				mDialog = DialogUtil.createDialog(RegActivity.this, null, null,
						null, msg, title);
			}
			mDialog.show();
		}
	};

	private PhoneNoLoader mPhoneNoLoader = new PhoneNoLoader() {
		@Override
		public String getPhoneNo() {
			return phoneNo;
		}
	};
	private CheckCodeCallBack callback = new CheckCodeCallBack() {

		@Override
		public void onGetCode(String code) {
			checkcode = code;
		}
	};
}
